#define _CRT_SECURE_NO_WARNINGS 1
#include "queue.h"

void queueInit(queue* q) {
	q->head = q->tail = NULL;
	q->size = 0;
}

void queuePush(queue* q, queueData num) {
	assert(q);
	qNode* newnode = (qNode*)malloc(sizeof(qNode));
	if (newnode == NULL) {
		perror("push->malloc");
		exit(-1);
	}
	newnode->data = num;
	newnode->next = NULL;
	if (queueEmpty(q)) {
		q->head = q->tail = newnode;
	}
	else {
		q->tail->next = newnode;
		q->tail = q->tail->next;
	}

	q->size++;
}

void queuePop(queue* q) {
	assert(q);
	assert(!queueEmpty(q));
	if (q->head->next == NULL) {
		free(q->head);
		q->head = q->tail = NULL;
	}
	else {
		qNode* next = q->head->next;
		free(q->head);
		q->head = next;
	}
	q->size--;
}

queueData queueFront(queue* q) {
	assert(q);
	assert(!queueEmpty(q));
	return q->head->data;
}

queueData queueBack(queue* q) {
	assert(q);
	assert(!queueEmpty(q));
	return q->tail->data;
}

int queueSize(queue* q) {
	assert(q);
	return q->size;
}

int queueEmpty(queue* q) {
	assert(q);
	return q->size == 0;
}

void queueDestory(queue* q) {
	assert(q);
	while (q->head) {
		qNode* next = q->head->next;
		free(q->head);
		q->head = next;
	}
	q->head = q->tail = NULL;
	q->size = 0;
}